package com.azxcrm.project.system.product.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.framework.web.controller.BaseController;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.framework.web.page.TableDataInfo;
import com.azxcrm.project.system.dict.domain.DictData;
import com.azxcrm.project.system.dict.service.IDictDataService;
import com.azxcrm.project.system.product.domain.ProductSpecification;
import com.azxcrm.project.system.product.service.IProductInfoService;
import com.azxcrm.project.system.product.service.IProductSpecificationService;

/**
 * 产品品种 控制层处理
 * 
 * @author Dustyone
 * @date 2018-09-04
 */
@Controller
@RequestMapping("/module/productSpecification")
public class ProductSpecificationController extends BaseController
{
	/**
     * 字典产品类型
     */
    @Value("${crm.dictionary.product.type.orign}")
    private String productType;
	
    private String prefix = "module/productSpecification";
	
	@Autowired
	private IProductSpecificationService productSpecificationService;
	
	@Autowired
	private IDictDataService dictDataService;
	
	@Autowired
	private IProductInfoService productInfoService;
	
	@GetMapping()
	@RequiresPermissions("module:productSpecification:view")
	String productSpecification()
	{
	    return prefix + "/productSpecification";
	}
	
	/**
	 * 查询产品品种列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("module:productSpecification:list")
	@ResponseBody
	public TableDataInfo list(ProductSpecification productSpecification)
	{
		setPageInfo(productSpecification);
        List<ProductSpecification> list = productSpecificationService.selectProductSpecificationList(productSpecification);
		return getDataTable(list);
	}
	
	/**
	 * 新增产品品种
	 */
	@GetMapping("/add")
	@RequiresPermissions("module:productSpecification:add")
	public String add(DictData dictData,Model model)
	{
		dictData.setDictType(productType);
		List<DictData> list = dictDataService.selectDictDataList(dictData);
		model.addAttribute("list", list);
	    return prefix + "/add";
	}

	/**
	 * 修改产品品种
	 */
	@GetMapping("/edit/{specId}")
	@RequiresPermissions("module:productSpecification:edit")
	public String edit(@PathVariable("specId") Integer specId,DictData dictData,Model model)
	{
		ProductSpecification productSpecification = productSpecificationService.selectProductSpecificationById(specId);
		model.addAttribute("productSpecification", productSpecification);
		dictData.setDictType(productType);
		List<DictData> list = dictDataService.selectDictDataList(dictData);
		model.addAttribute("list", list);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存产品品种
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("module:productSpecification:add")
	public Message save(ProductSpecification productSpecification)
	{
		if (productSpecificationService.saveProductSpecification(productSpecification) > 0)
		{
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 删除产品品种
	 */
	@PostMapping( "/remove/{specId}")
	@ResponseBody
	@RequiresPermissions("module:productSpecification:remove")
	public Message remove(@PathVariable("specId") Integer specId)
	{	
		if(productInfoService.checkProductSpecCasecaded(specId)){
			//如果该产品品种被引用过不予以被删除
			return Message.error(1, "该品种已被引用,不允许删除");
		}
		
		if (productSpecificationService.deleteProductSpecificationById(specId) > 0)
		{
		    return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除产品品种
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("module:productSpecification:batchRemove")
	public Message remove(@RequestParam("ids[]") Integer[] specIds)
	{
		for(int i = 0;i<specIds.length;i++){
			if(productInfoService.checkProductSpecCasecaded(specIds[i])){
				ProductSpecification productSpecification = productSpecificationService.selectProductSpecificationById(specIds[i]);
				//如果该工作流被应用过不予以被删除
				return Message.error(1, "产品品种 ["+productSpecification.getSpecName()+"] 已被引用,不允许删除");
			}
		}
		
		int rows = productSpecificationService.batchDeleteProductSpecification(specIds);
		if (rows > 0)
        {
            return Message.ok();
        }
        return Message.error();
	}
	
	/**
	 * 查询商品品种列表
	 */
	@GetMapping("/selectList")
	@ResponseBody
	public  List<ProductSpecification> selectList(ProductSpecification productSpecification)
	{
        List<ProductSpecification> list = productSpecificationService.selectProductSpecificationList(productSpecification);
		return list;
	}
	
}
